<template>
  <div>
    <el-card>
      <pageTools :show-left="false">
        <!-- <template #left>
        <span>有0条记录</span>
      </template> -->
        <template #right>
          <div>
            <el-button v-setButtonPower="'aa'" type="primary" @click="goUpload">
              导入
            </el-button>
            <el-button type="primary" @click="addClick">+新增员工 </el-button>
          </div>
        </template>
      </pageTools>
    </el-card>
    <el-card style="margin-top: 20px">
      <el-table :data="list">
        <el-table-column label="序号" width="50">
          <template v-slot="{ $index }">
            <div>{{ (page.page - 1) * page.size + $index + 1 }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="username" label="姓名" />
        <el-table-column prop="staffPhoto" label="员工头像">
          <template v-slot="{ row }">
            <img
              v-imgError="defaultImg"
              class="avatar"
              :src="row.staffPhoto"
              @click="ImgClick(row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column prop="mobile" label="手机号" />
        <el-table-column prop="workNumber" label="工号" />
        <el-table-column
          prop="formOfEmployment"
          :formatter="formatter"
          label="聘用形式"
          width="80"
        />
        <el-table-column prop="departmentName" label="部门" />
        <el-table-column
          prop="timeOfEntry"
          label="入职日期"
          :formatter="formatterTimeOfEntry"
        />
        <el-table-column label="操作" fixed="right" width="300">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="look(row)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="roleClick(row)">角色</el-button>
              <el-button type="text" @click="del(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 3, 10, 100, 200, 300, 400]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 添加弹框 -->
    <Add ref="add" @getData="getData" />
    <el-dialog title="二维码" :visible.sync="showQrcode">
      <div style="text-align: center">
        <canvas ref="canvas" />
      </div>
    </el-dialog>

    <Role ref="role" v-model="showRole" />
  </div>
</template>

<script>
import emloyeesData from '@/api/constant/employees'
import { sysUser } from '@/api/employees.js'
import Add from './components/add.vue'
import Role from './components/role.vue'
import dayjs from 'dayjs'
import { sysUserDel } from '@/api/employees'
import qrcode from 'qrcode'
export default {
  name: '',
  components: {
    Add,
    Role
  },
  data() {
    return {
      showRole: false,
      list: [],
      page: {
        page: 1, //	是	1	页码
        size: 10, //	是	10	每页条数
        total: 100
      },
      defaultImg: require('@/assets/common/head.jpg'),
      showQrcode: false
    }
  },
  computed: {},
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysUser(this.page)
      // console.log(res)
      this.page.total = res.data.total
      this.list = res.data.rows
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.page.size = val
      this.page.page = 1
      this.getData()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page.page = val
      this.getData()
    },
    // 数据转换
    formatter(row, colum, cellValue, index) {
      const temp = emloyeesData.hireType.find((item) => {
        return item.id === +cellValue
      })
      return temp ? temp.value : '-'
    },

    addClick() {
      this.$refs.add.dialogVisible = true
    },

    formatterTimeOfEntry(row, colum, cellValue, idnex) {
      return dayjs(cellValue).format('YYYY-MM-DD')
    },
    del(row) {
      this.$confirm('你确定要删除该员工吗？', '提示').then(async() => {
        await sysUserDel(row.id)
        this.$message.success('删除成功')
        this.handleCurrentChange(1)
      })
    },
    goUpload() {
      this.$router.push('/employees/upload?name=' + encodeURIComponent('员工'))
    },
    look(row) {
      // console.log(row)
      this.$router.push('/employees/detial/' + row.id)
    },
    // 点击头像生成二维码
    ImgClick(imgsrc) {
      if (!imgsrc) {
        return
      }
      this.showQrcode = true
      this.$nextTick(() => {
        qrcode.toCanvas(this.$refs.canvas, imgsrc, { with: 400, height: 400 })
      })
    },
    roleClick(row) {
      this.showRole = true
      this.$refs.role.getUsrInfo(row.id)
    }
  }
}
</script>
<style lang="scss" scoped>
.employsess {
  padding: 15px;
  .page {
    margin-top: 20px;
    text-align: right;
  }
  .avatar {
    width: 30px;
    height: 30px;
    object-fit: contain;
  }
}
</style>
